from common.config import *
import requests
import allure
from common.log_info import Logger
from common.pyredis import *
from data.redis_data import *
from tests.qwc_business_background.marketing.data import *
import pytest

market_log = Logger("activity", "activity").logger


@pytest.mark.skipif(pytest.global_env != 'test', reason="只在测试环境运行")
class Marketing:

    def get_token(self):
        data = PyRedis().get_value(minimall.get("DB"), minimall.get("minimall_key"), minimall.get("minimall_value"))
        return data.get("token")

    @allure.testcase("获取活动列表")
    def get_activity_list(self, env, mini_token):
        base_url = getconf(env, "qwc_url")
        url = base_url + "flashmall-retail-minimall-server/v1/activity/getList"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": mini_token}
        return requests.post(url, headers=headers)

    @allure.testcase("新增活动")
    def create_activity(self, env, qwc_token, data):
        base_url = getconf(env, "qwc_url")
        url = base_url + "flashmall-marketing-server/v1/activity/create"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": qwc_token}
        market_log.info(f"新增活动的请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    @allure.testcase("编辑活动")
    def update_activity(self, env, qwc_token, data):
        base_url = getconf(env, "qwc_url")
        url = base_url + "flashmall-marketing-server/v1/activity/update"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": qwc_token}

        market_log.info(f"新增活动的请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    @allure.testcase("查看活动")
    def activity_info(self, env, qwc_token, activity_id):
        base_url = getconf(env, "qwc_url")
        url = base_url + f"flashmall-marketing-server/v1/activity/getDetail/{activity_id}"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": qwc_token}
        market_log.info(f"查看活动的请求id：{activity_id}")
        return requests.get(url, headers=headers)

    @allure.testcase("活动列表")
    def activity_list(self, env, qwc_token):
        base_url = getconf(env, "qwc_url")
        url = base_url + f"flashmall-marketing-server/v1/activity/get/pageList"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": qwc_token}
        data = {
            "startTime": datetime.datetime.now().strftime("%Y-%m-%d 00:00:00"),
            "endTime": datetime.datetime.now().strftime("%Y-%m-%d 23:59:59"),
            "current": 1,
            "size": 10,
            "activityName": "独秀自动化测试"
        }
        market_log.info(f"查看活动列表的请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    @allure.testcase("删除活动")
    def activity_delete(self, env, qwc_token, activity_id):
        base_url = getconf(env, "qwc_url")
        url = base_url + f"flashmall-marketing-server/v1/activity/delete/{activity_id}"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": qwc_token}
        market_log.info(f"删除活动的请求id：{activity_id}")
        return requests.post(url, headers=headers)

    @allure.testcase("活动排序")
    def activity_sort(self, env, qwc_token, data):
        base_url = getconf(env, "qwc_url")
        url = base_url + "flashmall-marketing-server/v1/activity/updateSort"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": qwc_token}
        market_log.info(f"活动排序的请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    @allure.testcase("活动发布")
    def activity_release(self, env, qwc_token, activity_id):
        base_url = getconf(env, "qwc_url")
        url = base_url + f"flashmall-marketing-server/v1/activity/release/{activity_id}"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": qwc_token}
        market_log.info(f"活动发布的请求id：{activity_id}")
        return requests.post(url, headers=headers)

    @allure.testcase("活动撤销发布")
    def activity_recall(self, env, qwc_token, activity_id):
        base_url = getconf(env, "qwc_url")
        url = base_url + f"flashmall-marketing-server/v1/activity/recall/{activity_id}"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": qwc_token}
        market_log.info(f"活动撤销发布的请求id：{activity_id}")
        return requests.post(url, headers=headers)

    @allure.testcase("活动强制结束")
    def activity_kill(self, env, qwc_token, activity_id):
        base_url = getconf(env, "qwc_url")
        url = base_url + f"flashmall-marketing-server/v1/activity/kill/{activity_id}"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": qwc_token}
        market_log.info(f"活动强制结束的请求id：{activity_id}")
        return requests.post(url, headers=headers)

    @allure.testcase("获取商品spu_list")
    def goods_spu_list(self, env, qwc_token, data):
        base_url = getconf(env, "qwc_url")
        url = base_url + "flashmall-distribution-server/v1/goods-container/get-sales-spu-list"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": qwc_token}
        print("请求头", headers)
        market_log.info(f"获取商品spu列表的请求参数：{data}")

        return requests.post(url, headers=headers, json=data)

    @allure.testcase("获取商品spu_sku信息")
    def goods_sku_info(self, env, qwc_token, spu_id):
        base_url = getconf(env, "qwc_url")
        url = base_url + f"flashmall-marketing-server/v1/commodity/getSkuList/{spu_id}"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": qwc_token}
        market_log.info(f"请求的spu_id：{spu_id}")
        return requests.get(url, headers=headers)
